package com.yunfeisoft.controller.business;

import com.applet.base.BaseController;
import com.yunfeisoft.business.model.SmsCoupon;
import com.yunfeisoft.business.model.SmsCouponHistory;
import com.yunfeisoft.business.model.SmsCouponProductCategoryRelation;
import com.yunfeisoft.business.model.SmsCouponProductRelation;
import com.yunfeisoft.business.service.inter.SmsCouponHistoryService;
import com.yunfeisoft.business.service.inter.SmsCouponProductCategoryRelationService;
import com.yunfeisoft.business.service.inter.SmsCouponProductRelationService;
import com.yunfeisoft.business.service.inter.SmsCouponService;
import com.applet.utils.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * ClassName: SmsCouponController
 * Description: 优惠卷表Controller
 * Author: Jackie liu
 * Date: 2020-06-02
 */
@Controller
public class SmsCouponController extends BaseController {

    @Autowired
    private SmsCouponService smsCouponService;

    @Autowired
    private SmsCouponProductCategoryRelationService categoryRelationService;

    @Autowired
    private SmsCouponProductRelationService productRelationService;

    @Autowired
    private SmsCouponHistoryService couponHistoryService;

    /**
     * 添加优惠卷表
     *
     * @param couponStr
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/smsCoupon/save", method = RequestMethod.POST)
    @ResponseBody
    public Response save(String couponStr, HttpServletRequest request, HttpServletResponse response) {
        if (StringUtils.isBlank(couponStr)) {
            return ResponseUtils.success("参数错误");
        }
        smsCouponService.saveCoupon(couponStr);
        return ResponseUtils.success("保存成功");
    }

    /**
     * 修改优惠卷表
     *
     * @param couponStr
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/smsCoupon/modify", method = RequestMethod.POST)
    @ResponseBody
    public Response modify(String couponStr, HttpServletRequest request, HttpServletResponse response) {
        if (StringUtils.isBlank(couponStr)) {
            return ResponseUtils.success("参数错误");
        }
        Map<String, Object> result = smsCouponService.modifyCoupon(couponStr);
        return ResponseUtils.success(String.valueOf(result.get("message")), null);
    }

    /**
     * 查询优惠卷表
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/smsCoupon/query", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Response query(HttpServletRequest request, HttpServletResponse response) {
        String id = ServletRequestUtils.getStringParameter(request, "id", null);
//        if (StringUtils.isBlank(id)) {
//            return ResponseUtils.warn("参数错误");
//        }
        SmsCoupon record = smsCouponService.load(id);
        Map<String, Object> result = new HashMap<>();
        if (ObjectUtils.isEmpty(record)) {
            record = new SmsCoupon();
        } else {
            // 查询优惠券关联分类
            List<SmsCouponProductCategoryRelation> categoryRelationList = categoryRelationService.queryByCouponId(record.getId());
            record.setCategoryRelationList(categoryRelationList);
            // 查询优惠券关联商品
            List<SmsCouponProductRelation> productRelationList = productRelationService.queryByCouponId(record.getId());
            record.setProductRelationList(productRelationList);
            // 查询优惠券使用记录
            List<SmsCouponHistory> couponHistoryList = couponHistoryService.queryByCouponId(record.getId());
            record.setCouponHistoryList(couponHistoryList);
        }
        result.put("record", record);
        // 优惠卷类型列表
        SmsCoupon.CouponTypeEnum[] couponTypeEnums = SmsCoupon.CouponTypeEnum.values();
        List<Map<String, Object>> typeList = new ArrayList<>();
        Arrays.asList(couponTypeEnums).stream().forEach(couponTypeEnum -> {
            Map<String, Object> map = new HashMap<>();
            map.put("name", couponTypeEnum.getLabel());
            map.put("value", couponTypeEnum.getValue());
            typeList.add(map);
        });
        result.put("typeList", typeList);
        // 使用平台列表
        SmsCoupon.CouponPlatformEnum[] couponPlatformEnums = SmsCoupon.CouponPlatformEnum.values();
        List<Map<String, Object>> platformList = new ArrayList<>();
        Arrays.asList(couponPlatformEnums).stream().forEach(couponPlatformEnum -> {
            Map<String, Object> map = new HashMap<>();
            map.put("name", couponPlatformEnum.getLabel());
            map.put("value", couponPlatformEnum.getValue());
            platformList.add(map);
        });
        result.put("platformList", platformList);
        return ResponseUtils.success(result);
    }

    /**
     * 分页查询优惠卷表
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/smsCoupon/list", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Response list(HttpServletRequest request, HttpServletResponse response) {
        String name = ServletRequestUtils.getStringParameter(request, "name", null);
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("name", name);
        initParams(params, request);
        Page<SmsCoupon> page = smsCouponService.queryPage(params);
        return ResponseUtils.success(page);
    }

    /**
     * 批量删除优惠卷表
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/smsCoupon/delete", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Response delete(HttpServletRequest request, HttpServletResponse response) {
        String ids = ServletRequestUtils.getStringParameter(request, "ids", null);
        if (StringUtils.isBlank(ids)) {
            return ResponseUtils.warn("参数错误");
        }
        smsCouponService.remove(ids.split(","));
        return ResponseUtils.success("删除成功");
    }
}
